package com.itheima.sky.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.sky.dao.SetMealMapper;
import com.itheima.sky.dto.SetmealDTO;
import com.itheima.sky.dto.SetmealSaveDTO;
import com.itheima.sky.entity.PageResult;
import com.itheima.sky.entity.Result;
import com.itheima.sky.mapper.SetmealDishMapper;
import com.itheima.sky.pojo.Setmeal;
import com.itheima.sky.pojo.SetmealDish;
import com.itheima.sky.service.SetmealService;
import com.itheima.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description DishServiceImpl
 * @Author oumei
 * @Date 2024-06-27
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetMealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 查询分页套餐页面
     * @param setmealDto
     * @return
     */
    @Override
    public PageResult<SetmealVO> findSetmealPageAll(SetmealDTO setmealDto) {

        //获取页数和每页显示条数
        PageHelper.startPage(setmealDto.getPage(),setmealDto.getPageSize());
        //查询所有数据并通过name进行查找
        List<SetmealVO> setmealList = setmealMapper.findSetmalList(setmealDto.getName());

        //将集合封装到PageInfo中
        PageInfo<SetmealVO> pageInfo = new PageInfo<>(setmealList);

        //将PageInfo封装到PageResult里
        PageResult<SetmealVO> setmealPageResult = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());

        return setmealPageResult;
    }


    /**
     * 新增套餐
     *
     * @param setmealSaveDTO
     */
    @Override
    public void save(SetmealSaveDTO setmealSaveDTO) {
        //1. 补全setmeal的修改、创建时间信息
        setmealSaveDTO.setCreateTime(LocalDateTime.now());
        setmealSaveDTO.setUpdateTime(LocalDateTime.now());
        //调用保存的方法
        setmealMapper.save(setmealSaveDTO);

        //3.得到套餐的菜品，给所有的菜品补全信息(setmeal_id 、修改、创建时间、排序，)
        Snowflake snowflake = IdUtil.createSnowflake(1, 2);
        List<SetmealDish> setmealDishes = setmealSaveDTO.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(setmealDish -> {
            setmealDish.setId(snowflake.nextId());
            setmealDish.setSetmealId(setmealSaveDTO.getId());
            setmealDish.setCreateUser(setmealSaveDTO.getCreateUser());
            setmealDish.setUpdateUser(setmealSaveDTO.getUpdateUser());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setSort(0);
            return setmealDish;
        }).collect(Collectors.toList());

        //批量插入到菜品表
        setmealDishMapper.saveBatch(setmealDishes);
    }

    /**
     * 删除
     *
     * @param ids
     */
    @Override
    public Result delete(List<Long> ids) {

        //先查出套餐总数
        Long count = setmealMapper.queryDishWithStatus(ids);



        //1.检查要删除的套餐里面有没有在售,如果存在着在售套餐，直接抛出异常，不能删除
        if (count > 0) {
            return Result.error("存在套餐是在售状态，无法删除");
//            throw new BaseException("存在套餐是在售状态，无法删除");
        }

        //2. 如果要删除套餐都是停售，那么就可以直接删除套餐
        setmealMapper.deleteByIds(ids);

        //3. 删除套餐对应的套餐菜品
        setmealDishMapper.deleteBySetmealIds(ids);
        return Result.success("删除成功");
    }


    /**
     * 批量起售成功
     *
     * @param ids
     */
    @Override
    public void updatestatus1(List<Long> ids) {

        setmealMapper.updatestatus1(ids);

    }

    /**
     * 批量停售成功
     *
     * @param ids
     */
    @Override
    public void updatestatus0(List<Long> ids) {

        setmealMapper.updatestatus0(ids);
    }

    /**
     * 获取修改前的数据
     *
     * @param id
     */
    @Override
    public SetmealSaveDTO findBeforeById(Long id) {

        SetmealSaveDTO setmealSaveDTO=new SetmealSaveDTO();
        Setmeal setmeal = setmealMapper.findBeforeById(id);
        BeanUtils.copyProperties(setmeal,setmealSaveDTO);
        List<SetmealDish> setmealDishes = setmealMapper.selectSetmealDishById(id);
        setmealSaveDTO.setSetmealDishes(setmealDishes);
        return setmealSaveDTO;
    }

    /**
     * 通过id修改数据
     * @param setmealSaveDTO
     */
    @Override
    public void updateById(SetmealSaveDTO setmealSaveDTO) {

        setmealSaveDTO.setUpdateTime(LocalDateTime.now());
        setmealMapper.updateById(setmealSaveDTO);
        setmealMapper.deleteByDishId(setmealSaveDTO.getId());
        Snowflake snowflake = IdUtil.createSnowflake(1, 2);
        //3.得到套餐的菜品，给所有的菜品补全信息(setmeal_id 、修改、创建时间、排序，)
        List<SetmealDish> setmealDishes = setmealSaveDTO.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(setmealDish -> {
            setmealDish.setId(snowflake.nextId());
            setmealDish.setSetmealId(setmealSaveDTO.getId());
            setmealDish.setCreateUser(setmealSaveDTO.getCreateUser());
            setmealDish.setUpdateUser(setmealSaveDTO.getUpdateUser());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setSort(0);
            return setmealDish;
        }).collect(Collectors.toList());
        //批量插入到菜品表
        setmealDishMapper.saveBatch(setmealDishes);
    }
}
